package com.example.photograph.controller;

import com.example.photograph.domain.AjaxResult;
import com.example.photograph.domain.SysNotice;
import com.example.photograph.domain.SysOrder;
import com.example.photograph.page.TableDataInfo;
import com.example.photograph.service.ISysNoticeService;
import com.example.photograph.service.ISysOrderService;
import com.example.photograph.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 公告 信息操作处理
 * 
 * @author wuyj
 */
@Controller
@RequestMapping("/system/notice")
@EnableScheduling
public class SysNoticeController extends BaseController
{
    private String prefix = "system/notice";

    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private ISysOrderService orderService;

    @RequiresPermissions("system:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询公告列表
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公告
     */
    @RequiresPermissions("system:notice:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysNotice notice)
    {
        notice.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap)
    {
        mmap.put("notice", noticeService.selectNoticeById(noticeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:notice:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysNotice notice)
    {
        notice.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除公告
     */
    @RequiresPermissions("system:notice:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }

    /**
     * 定时发送通知
     */
    @Scheduled(fixedRate=60000)
    public void timingAdd()
    {
        SysOrder orderParam = new SysOrder();
        orderParam.setGetTime(new Date());
        List<SysOrder> orderList = orderService.selectSysOrderList(orderParam);
        for (SysOrder order: orderList) {
            Date date=new Date();
            DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            SysNotice noticeParam = new SysNotice();
            noticeParam.setUserId(order.getUserId());
            noticeParam.setNoticeTitle("取片通知 "+format.format(date));
            noticeParam.setNoticeType("1");
            noticeParam.setNoticeContent("今天是您约定的取片日期，不要忘记哦！");
            noticeParam.setStatus("0");
            noticeService.insertNotice(noticeParam);
        }
    }

    public static void main(String[] args) {

        System.out.println(LocalDateTime.now());
    }
}
